function onModuleLoad() {
	$("#accordion").accordion('activate', 2);
	var $authorityModule = $("#authorityModule");
	var $authority_add_form = $("#authorityModule_add_form");
	var $authorityTable = $("#authorityModule_table");
	var $serverErrormessage;
	var deleteAuthorityName;
	var deleteAuthorityUrl;
	var deleteAuthorityId;
	var authorityRowToDelete;
	// following script stops the submit event of form when user clicks the Enter button
	$('.noEnterSubmit').keypress(function(e){
	  	if ( e.which == 13 ) return false;
	  	
	});
	var $authority_add_dialog = $( "#authorityModule_add_dialog" ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Add: function() {
				if($authority_add_form.valid())
				{
				    var url = $authority_add_form.attr( 'action' );
				    var jqxhr = $.post(url,$authority_add_form.serialize(), function(data) {
				   			
							  if(data.status == "Success"){
								  $authorityTable.append("<tr> <td>" +data.resultMap.newAuthorityName + "</td> <td><a class=\"authorityModuleDelete\" href=\"../delete/$" +data.resultMap.newAuthorityId +"\">Delete</a></td> </tr>");
									 
								  $authority_add_dialog.dialog( "close" );
								  
								  $("#authorityModule p").remove();
								  
							  }
					      else {
					    	  $serverErrormessage = $( "#authorityModule_add_dialog_status" , $authority_add_dialog);
					    	  $serverErrormessage.text("Error adding new district - " + data.errorMap.errorMessage);
					      }					   	  
					    }, "json")
					    .success(function() { alert("second success"); })
					    .error(function() { alert("error"); })
					    .complete(function() { alert("complete"); 
					    $( this ).dialog( "close" );
					    });					
				}
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			$tab_title_input.focus();
			$serverErrormessage.text("");
		},
		close: function() {
			$district_add_form[ 0 ].reset();
		}
	});
	
	// addTab button: just opens the dialog
	$( "#authorityModule_add_button" ).click(function() {
		$authority_add_dialog.dialog( "open" );
		});
	
	var $authority_delete_dialog = $("#authorityModule_delete_dialog", $authorityModule ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Yes: function() {
				var jqxhrDelete = $.post(deleteAuthorityUrl,{authorityId:deleteAuthorityId}, function(data) {
					if(data.status == "Success"){
						authorityRowToDelete.remove();
						  $authority_delete_dialog.dialog( "close" );
				    	
				      }
				      else {
				    	  $serverErrormessage = $( "#authorityModule_delete_dialog_status" , $authority_delete_dialog);
				    	  $serverErrormessage.text("Error deleting the authority - " + data.errorMap.errorMessage);
				      }
				    	  
				    }, "json");								
			},
			No: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			//$tab_title_input.focus();
		},
		close: function() {
			$serverErrormessage.text("");
		}
	});
	
	var $authorityDeleteLink = $("a", $authorityTable);
	$authorityDeleteLink.live("click",function(event) {
        event.preventDefault();
        authorityRowToDelete = $(this).parent().parent();
        deleteAuthorityName = $(this).parent().siblings().html();
        deleteAuthorityUrl = $(this).attr('href');
        var hrefArray= deleteAuthorityUrl.split('$'); 
        deleteAuthorityUrl = hrefArray[0];
        deleteAuthorityId = hrefArray[1];
        $("#authorityModule_delete_message",$authority_delete_dialog).text(deleteAuthorityName);
        $authority_delete_dialog.dialog( "open" );
       
    });
};